﻿
#include "CreateCircleDlg.h"
#include <vtkArcSource.h>
#include <vtkActor.h>    
#include <vtkPolyDataMapper.h>    



CreateCircleDlg::CreateCircleDlg(QWidget* parent):
    DlgBase(parent)
{
    initUI();
    manageConnections();
}

CreateCircleDlg::~CreateCircleDlg()
{
}

bool CreateCircleDlg::create()
{
    auto pVtkViewWindow = MyVtkViewWindow::GetInstance();
    if(pVtkViewWindow == nullptr)
    {
        return false;
    }

    {
        m_centerPointCoord->getValues(m_centerPoint[0], m_centerPoint[1], m_centerPoint[2]);
        m_normalVectorCoord->getValues(m_normalVector[0], m_normalVector[1], m_normalVector[2]);
        m_radiusValue = m_radiusEdit->getValue();

        // calculate polar vector
        if(m_normalVector[0] != 0 || m_normalVector[1] != 0)
        {
            m_polarVector[0] = m_normalVector[0];
            m_polarVector[1] = -m_normalVector[1];
            m_polarVector[2] = 0;
        }
        else
        {
            m_polarVector[0] = 1;
            m_polarVector[1] = 0;
            m_polarVector[2] = 0;
        }

        vtkMath::Normalize(m_normalVector);
        vtkMath::Normalize(m_polarVector);
        vtkMath::MultiplyScalar(m_polarVector, m_radiusValue);
        m_polarVector[0] = m_centerPoint[0] + m_polarVector[0];
        m_polarVector[1] = m_centerPoint[1] + m_polarVector[1];
        m_polarVector[2] = m_centerPoint[2] + m_polarVector[2];



        // vtk vtkArcSource
        vtkNew<vtkArcSource> arcSource;
        arcSource->SetCenter(m_centerPoint);
        arcSource->UseNormalAndAngleOn();
        arcSource->SetNormal(m_normalVector);
        arcSource->SetPolarVector(m_polarVector);
        arcSource->SetAngle(360);
        arcSource->SetResolution(100);
        arcSource->Update();

        // vtk vtkPolyDataMapper
        vtkNew<vtkPolyDataMapper> mapper;
        mapper->SetInputConnection(arcSource->GetOutputPort());
        mapper->StaticOn();

        // vtk vtkActor
        vtkNew<vtkActor> actor;
        actor->SetMapper(mapper);
        actor->GetProperty()->SetColor(QtVtkModle::GlobalValue::CircleLineColor);
        actor->GetProperty()->SetLineWidth(QtVtkModle::GlobalValue::GetCircleLineWidth());

        //add line to vtk view
        pVtkViewWindow->GetRenderer()->AddActor(actor);
        GetActorManagerInstance()->AddActor(actor, PickMode::EDGE);
    }

    pVtkViewWindow->GetRenderer()->ResetCamera();
    pVtkViewWindow->GetRenderWindow()->Render();
    return true;
}

void CreateCircleDlg::initUI()
{
    setWindowTitle("Create Circle");
    QVBoxLayout* pDlgLayout = new QVBoxLayout(this);  //create layout

    {
        m_centerPointCoord = new MyCoordinateEdit(this, "Center Point");
        pDlgLayout->addWidget(m_centerPointCoord);

        m_normalVectorCoord = new MyCoordinateEdit(this, "Normal Vector");
        m_normalVectorCoord->setValues(0, 0, 1);
        pDlgLayout->addWidget(m_normalVectorCoord);

        m_radiusEdit = new MyDoubleEdit(this, "Radius");
        m_radiusEdit->setValue(1);
        pDlgLayout->addWidget(m_radiusEdit);
    }

    {
        m_okBtn = new QPushButton("OK", this);
        m_cancelBtn = new QPushButton("Cancel", this);

        QHBoxLayout* okCancelLayout = new QHBoxLayout(this);
        okCancelLayout->addWidget(m_okBtn);
        okCancelLayout->addWidget(m_cancelBtn);
        pDlgLayout->addLayout(okCancelLayout);
    }

    this->setLayout(pDlgLayout);
}

void CreateCircleDlg::manageConnections()
{
    connect(m_okBtn, &QPushButton::clicked, this, [=]()
            {
                if(create())
                {
                    close();
                }
                else
                {
                    QMessageBox::warning(this, "Error", "Failed to create circle!");
                }

            });

    connect(m_cancelBtn, &QPushButton::clicked, this, &CreateCircleDlg::close);
}



